﻿using Currency.WebApi.Shared.Extension;
using System;

namespace Microsoft.Extensions.DependencyInjection
{
    /// <summary>
    /// 扩展注入
    /// </summary>
    public static class ServiceCollectionExtension
    {
        /// <summary>
        /// 统一注册Webpi通用服务
        /// </summary>
        /// <typeparam name="TPermissionHandler"></typeparam>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        /// <param name="environment"></param>
        /// <param name="serviceInfo"></param>
        /// <param name="completedExecute"></param>
        /// <returns></returns>
        public static IServiceCollection AddServices(this IServiceCollection services, Action<ServicesRegister> completedExecute = null)
        {
            var configuration = services.GetConfiguration();
            var serviceInfo = services.GetServiceInfo();
            var environment = services.GetHostEnvironment();
            services.AddControllers();
            services.AddHttpContextAccessor();
            services.AddMemoryCache();

            var _srvRegistration = new ServicesRegister(configuration, services, environment, serviceInfo);
            _srvRegistration.Configure();//配置
            _srvRegistration.AddEfCoreContext();//注册注册EfcoreContext
            _srvRegistration.AddSwaggerGen();//注册SwaggerGen

            completedExecute?.Invoke(_srvRegistration);

            return services;
        }
    }
}
